<?php

/**
 * Contrôleur de gestion des données
 */
class gestion_controller extends Component
{
    /**
     * Constructeur
     */
    public function __construct()
    {
        /* NEED CONNECT */
        $connect = Base::Load(CLASS_USER)->isConnect();
        if ($connect == false) {
            header('location: '.SYS_FOLDER);
        }
    }

    public function defaut()
    {
        $this->_view->addBlock('content','defaut.tpl');
    }

    public function addShop(){
        $this->_view->assign('regions', Base::Load(CLASS_LOC)->getRegion());
        $this->_view->addBlock('data','addShop.tpl');
    }

    public function POST_newShop($data)
    {
        $v = Base::Load(CLASS_LOC)->getVille($data['ville']);
        $gps = Tools::getGPS($v[2], $data['adress']);
        $data['lat'] = $gps->lat;
        $data['lng'] = $gps->lng;
        Base::Load(CLASS_SHOP)->save($data);
    }

    public function addProduct()
    {
        $this->_view->addBlock('data','addProduct.tpl');
    }

    public function POST_newProduct($data)
    {
        Base::Load(CLASS_PRODUCT)->save($data);
    }

    public function addCategorie()
    {
        $this->_view->addBlock('data','addCategorie.tpl');
    }

    public function POST_newCategorie($data)
    {
        Base::Load(CLASS_PROD_CAT)->save($data);
    }

    public function addMarque()
    {
        $this->_view->addBlock('data','addMarque.tpl');
    }

    public function POST_newMarque($data)
    {
        Base::Load(CLASS_PROD_MARQUE)->save($data);
    }

    public function addReleve()
    {
        $magasin = Base::Load(CLASS_SHOP)->getAll();
        
        foreach($magasin as $i => $m) {
            $magasin[$i]['ville'] = Base::Load(CLASS_LOC)->getVille($m['ville']);
        }
        
        $this->_view->assign('magasins', $magasin);

        $rayons = Base::Load(CLASS_PROD_RAYON)->getAll();
        $this->_view->assign('rayons', $rayons);

        $categories = Base::Load(CLASS_PROD_CAT)->getAll();
        $this->_view->assign('categories', $categories);

        $marques = Base::Load(CLASS_PROD_MARQUE)->getAll();
        $this->_view->assign('marques', $marques);

        $condits = Base::Load(CLASS_PRODUCT_COND)->getAll();
        $this->_view->assign('condits', $condits);

        $this->_view->addBlock('data','addReleve.tpl');
    }

    public function POST_newReleve($data)
    {
        unset($data['productSF']);
        unset($data['u']);
        $data['product'] = json_decode($data['product'])->id;

        $data['prix'] = number_format ($data['prix'], 2);
        $p = Base::Load(CLASS_PRODUCT)->get($data['product']);
        $data['ProductKEY'] = $p['name'];
        $data['chasseur'] = $_SESSION['user']['uid'];
        Base::Load(CLASS_RELEVE)->save($data);
    }
}


/* -- Fin du fichier -- */